# -*- coding: utf-8 -*-
#
# encode-xvid - Encode de vídeos em formato mpeg4 (xvid)/mp3
# URLs: http://code.google.com/p/encode-xvid/, http://estacaozn.blogspot.com
# Copyright 2011 Felipe Guerço Oliveira - felipeguerco@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Janela principal da aplicação

import gtk
from ScriptRunner import ScriptRunner

class MainWindow(gtk.Window):
	# script que será executado
	script = ""

	# campos da tela
	txt_video = gtk.Entry()
	txt_legenda = gtk.Entry()
	txt_vbitrate = gtk.Entry()
	txt_abitrate = gtk.Entry()
	txt_volume = gtk.Entry()
	txt_output = gtk.Entry()
	chk_simulacao = gtk.CheckButton("Modo simulação")
	
	
	# construtor - monta a interface
	def __init__(self, parent=None):
		gtk.Window.__init__(self)

		self.connect('destroy', lambda *w: gtk.main_quit())
        
		self.set_title('Encode Xvid - por Felipe Guerço')
		self.set_border_width(10)

		frame = gtk.Frame('Parâmetros para o script')
		self.add(frame)

		panel = gtk.VBox()
		frame.add(panel)

		panel.pack_start(self.criar_interface(), padding=10)
		panel.pack_end(self.criar_barra_botoes(), expand=False, fill=False)

		self.set_position(gtk.WIN_POS_CENTER)
		self.show_all()

	
	def executar_script(self):
		runner = ScriptRunner()
		runner.run(self.script,
				self.txt_video.get_text(),
				self.txt_legenda.get_text(),
				self.txt_vbitrate.get_text(),
				self.txt_abitrate.get_text(),
				self.txt_volume.get_text(),
				self.txt_output.get_text(),
				self.chk_simulacao.get_active())


	# callback - exibe a caixa de diálogo de abrir ou salvar arquivo e atualiza um Entry.
	def selecionar_arquivo(self, widget, acao_arquivo, textbox):
		dialog = gtk.FileChooserDialog("Selecione o arquivo",
										self,
	        							acao_arquivo,
	        	      					(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
	        		  						gtk.STOCK_SAVE, gtk.RESPONSE_OK))
		
		dialog.set_default_response(gtk.RESPONSE_OK)
		response = dialog.run()
	
		if response == gtk.RESPONSE_OK:
			textbox.set_text(dialog.get_filename())
	
		dialog.destroy()
	  

	# cria um container com um label, um entry e um botão para selecionar o arquivo
	def criar_escolha_arquivo(self, titulo, textbox, acao_arquivo):
		label = gtk.Label(titulo)
		label.set_alignment(0, 0)

		box = gtk.HBox()
		box.set_spacing(5)

		box.pack_start(textbox)

		btn = gtk.Button("...")
		btn.connect("clicked", self.selecionar_arquivo, acao_arquivo, textbox)

		box.pack_end(btn, False)

		
		panel = gtk.VBox()
		panel.pack_start(label, padding = 5)
		panel.pack_start(box, padding = 5)
		
		return panel
	

	# cria um container com um label e um entry
	def criar_campo_texto(self, box, label, textbox):
		box.pack_start(gtk.Label(label), False)
		textbox.set_size_request(60, -1)
		box.pack_start(textbox, False)
	
	# define a interface
	def criar_interface(self):
		panel = gtk.VBox()
		panel.set_size_request(600, -1)
		panel.set_border_width(10)

		panel.pack_start(self.criar_escolha_arquivo("Arquivo de vídeo",
				self.txt_video, gtk.FILE_CHOOSER_ACTION_OPEN), False)
		panel.pack_start(self.criar_escolha_arquivo("Arquivo de legenda",
				self.txt_legenda, gtk.FILE_CHOOSER_ACTION_OPEN), False)
		
		box = gtk.HBox()
		box.set_spacing(10)
		
		self.criar_campo_texto(box, "Vídeo bitrate:", self.txt_vbitrate)
		self.criar_campo_texto(box, "Áudio bitrate:", self.txt_abitrate)
		self.criar_campo_texto(box, "Volume:", self.txt_volume)
		
		box.pack_start(self.chk_simulacao, False, padding = 5)
		panel.pack_start(box, False, padding = 20)
		
		panel.pack_start(self.criar_escolha_arquivo("Arquivo de saída",
				self.txt_output, gtk.FILE_CHOOSER_ACTION_SAVE), False)

		return panel

	# cria a barra de botões padrão
	def criar_barra_botoes(self):
		buttons_panel = gtk.HButtonBox()
		buttons_panel.set_layout(gtk.BUTTONBOX_END)
		buttons_panel.set_spacing(10)
		buttons_panel.set_border_width(10)

		button = gtk.Button(stock='gtk-ok')
		button.connect("clicked", lambda *w: self.executar_script())
		buttons_panel.add(button)

		button = gtk.Button(stock='gtk-cancel')
		button.connect('clicked', lambda *w: gtk.main_quit())
		buttons_panel.add(button)

		return buttons_panel
